﻿
using System;
using System.Collections.Generic;

namespace sharpxbot.Core.Security.Traffic.Counters
{
    /// <summary>
    /// Represents day counters. Contains 24 hour counters
    /// </summary>
    public class DayCounters : BaseCounters
    {
        /// <summary>
        /// Gets day value
        /// </summary>
        public int Day { get; private set; }

        /// <summary>
        /// Gets array of hour counters
        /// </summary>
        public HourCounters[] Hours { get; private set; }

        /// <summary>
        /// Creates new day counters using given day value and hour counters array
        /// </summary>
        /// <param name="day">Must be from 0 to 31. (all bounds inclusive)</param>
        /// <param name="hours">Array of hour counters. Must be valid array of 24 items</param>
        public DayCounters(int day, HourCounters[] hours)
            :base()
        {
            Validator.ValidateDay(day);

            if (hours == null)
                throw new NullReferenceException("Illegal hours array. Null-reference");

            if (hours.Length < Constants.HoursCount)
                throw new ArgumentException(String.Format("Illegal hours counters array. Must be {0} length", Constants.HoursCount));

            this.Day = day;
            this.Hours = hours;
        }

        /// <summary>
        /// Creates new day counters using given day and initializes hour counters
        /// </summary>
        /// <param name="day">Must be from 0 to 31. (all bounds inclusive)</param>
        public DayCounters(int day)
            :base()
        {
            Validator.ValidateDay(day);

            this.Day = day;

            InitHours();
        }

        /// <summary>
        /// Creates new day counters using current system time and initializes hour counters
        /// </summary>
        public DayCounters()
            :base()
        {
            this.Day = DateTime.Now.Day;

            InitHours();
        }

        #region Private methods

        private void InitHours()
        {
            List<HourCounters> hours = new List<HourCounters>();

            for (int i = 0; i < Constants.HoursCount; ++i)
                hours.Add(new HourCounters(i));

            this.Hours = hours.ToArray();
        }

        #endregion
    }
}
